package com.byxm.wlgw.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.byxm.wlgw.bean.SpxxBean;
import com.byxm.wlgw.bean.Yhxx_glBean;
import com.byxm.wlgw.common.DataBaseAccess;
import com.byxm.wlgw.query.Yhxx_glQuery;

public class Yhxx_glService {
	Yhxx_glQuery query=new Yhxx_glQuery();
	DataBaseAccess dba=null;
	PreparedStatement ps=null;
	/**
	 * 查看个人用户信息
	 * @param yhxxglid
	 * @return
	 */
	public Yhxx_glBean findYhxxglBean(int yhxxglid){
		Yhxx_glBean yhxxBean=new Yhxx_glBean();
		ResultSet rs=null;
		try {
			dba =new DataBaseAccess();
			rs=dba.executeQuery(query.findYHxxglByid(yhxxglid));
			while(rs.next()){
				yhxxBean.setYhxxglid(rs.getInt("yhxxglid"));
				yhxxBean.setYhxxglzh(rs.getString("yhxxglzh"));
				yhxxBean.setZsxm(rs.getString("zsxm"));
			    yhxxBean.setYgbh(rs.getString("ygbh"));
			    yhxxBean.setYhnc(rs.getString("yhnc"));
			    yhxxBean.setBm(rs.getString("bm"));
			    yhxxBean.setZw(rs.getInt("zw"));
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return yhxxBean;
	}

	/**
	 * 按条件检索用户信息
	 * @param yhxxglBean
	 * @return
	 */
	public List<Yhxx_glBean> findYhxxgllst(String searchString){
		String sql=query.findYhxx(searchString);
		ResultSet rs=null;
		List<Yhxx_glBean> lstyhxx=new ArrayList<Yhxx_glBean>();
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				Yhxx_glBean glBean=new Yhxx_glBean();
				glBean.setYhxxglid(rs.getInt("yhxxglid"));
				glBean.setYhxxglzh(rs.getString("yhxxglzh"));
				glBean.setZsxm(rs.getString("zsxm"));
				glBean.setYgbh(rs.getString("ygbh"));
				glBean.setYhnc(rs.getString("yhnc"));
				glBean.setBm(rs.getString("bm"));
				glBean.setZw(rs.getInt("zw"));
				lstyhxx.add(glBean);
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return lstyhxx;
	}
	
	/**
	 * 修改用户信息
	 * @param yGlBean
	 * @return
	 * @throws Exception
	 */
	public boolean updateYhxx(Yhxx_glBean yGlBean) throws Exception{
		int iFlag=0;
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(query.updateYhxxgl());
			ps.setString(1, yGlBean.getYhxxglzh());
			ps.setString(2, yGlBean.getYhglmm());
			ps.setString(3, yGlBean.getZsxm());
			ps.setString(4, yGlBean.getYhnc());
			ps.setString(5, yGlBean.getBm());
			ps.setString(6, yGlBean.getYgbh());
			ps.setInt(7, yGlBean.getYhxxglid());
			iFlag=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
	     if(iFlag>0){
	    	 return true;
	     }else{
	    	 return false;
	     }
	}
	
	/**
	 * 删除用户信息
	 * @param yhxxglid
	 * @return
	 * @throws Exception
	 */
	public boolean deleteYhxx(int yhxxglid) throws Exception{
		int isFlag=0;
		try {
			dba=new DataBaseAccess();
			isFlag=dba.executeUpdate(query.deleteYhxxgl(yhxxglid));
			dba.commit();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(isFlag>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 用户登录信息
	 * @param yhxxglBean
	 * @return
	 */
	public Yhxx_glBean findYhxxgl(Yhxx_glBean yhxxglBean){
		ResultSet rs=null;
		Yhxx_glBean yhxxglBean2=new Yhxx_glBean();
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(query.findYhxxgl());
			ps.setString(1, yhxxglBean.getYhxxglzh());
			ps.setString(2, yhxxglBean.getYhglmm());
			rs=ps.executeQuery();
			if(rs.next()){
				yhxxglBean2.setYhxxglid(rs.getInt("yhxxglid"));
				yhxxglBean2.setYhxxglzh(rs.getString("yhxxglzh"));
				//暂时写2个
			}else{
				//为了保险
				yhxxglBean2.setYhxxglzh("");
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return yhxxglBean2;
	}
	/**
	 * 保存用户信息
	 * @return
	 */
	public boolean saveYhxx_gl(Yhxx_glBean yGlBean){
		int iFlag=0;
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(query.getSaveYhxx_glSql());
			ps.setString(1, yGlBean.getYhxxglzh());
			ps.setString(2, yGlBean.getYhglmm());
			ps.setString(3, yGlBean.getZsxm());
			ps.setString(4, yGlBean.getYhnc());
			ps.setString(5, yGlBean.getYgbh());
			ps.setString(6, yGlBean.getBm());
			ps.setInt(7, yGlBean.getZw());
			iFlag=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			try {
				dba.rollback();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(iFlag>0){
			return true;
		}else{
			return false;
		}
	}
}
